//
//  TableSectionItem.swift
//  Pods-TSKit_Example
//
//  Created by chenyusen on 2018/3/16.
//

import UIKit

open class TableSectionItem: NSObject {

    open var model: Any?
    open var headerTitle: String?
    open var footerTitle: String?
    
    open var headerHeight: CGFloat?
    open var footerHeight: CGFloat?
    
    open weak var currentSectionView: TableSectionView?
    
    open var headerSectionViewClass: TableSectionView.Type?
    open var footerSectionViewClass: TableSectionView.Type?
    
    open var rows: [TableCellItem]
    
    init(model: Any? = nil,
         headerTitle: String? = nil,
         footerTitle: String? = nil,
         headerHeight: CGFloat? = nil,
         footerHeight: CGFloat? = nil,
         headerSectionViewClass: TableSectionView.Type? = nil,
         footerSectionViewClass: TableSectionView.Type? = nil,
         rows: [TableCellItem]? = nil
        ) {
        self.headerTitle = headerTitle
        self.footerTitle = footerTitle
        self.headerHeight = headerHeight
        self.footerHeight = footerHeight
        self.model = model
        self.headerSectionViewClass = headerSectionViewClass
        self.footerSectionViewClass = footerSectionViewClass
        self.rows = rows ?? []
    }
}


open class TableSectionView: UITableViewHeaderFooterView {
    
    class var shouldAppendSectionItemClassToReuseIdentifier: Bool {
        return false
    }
    
    // 当前SectionView所持有的sectionItem
    var sectionItem: TableSectionItem?
    
    
    /// 更新数据
    func updateSection(sectionItem: TableSectionItem?) {
        self.sectionItem = sectionItem
        sectionItem?.currentSectionView = self
    }
    
    open override func prepareForReuse() {
        if sectionItem?.currentSectionView == self {
            sectionItem?.currentSectionView = nil
        }
        sectionItem = nil
        super.prepareForReuse()
    }
}


class TestSectionView: TableSectionView {
    override class var shouldAppendSectionItemClassToReuseIdentifier: Bool {
        return true
    }
}
